#!/bin/bash
# Author    : some-guy (Muhammed U)
# Copyright : (C) 2008 some-guy
# E-mail    : muhammedu (at) gmail (dot) com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# Requires libslaxget

## Download a file
# $1 == File URL
# $2 == append / output / stdout(verbose)
# $3 == Mode: quiet/verbose
slapt_retrieve_file()
{
FILE_URL=$1
FILE_APPEND=$2
FILE_DOWN_MANAGER=$3
if [ $FILE_DOWN_MANAGER == "verbose" ]; then
	if [ $2 != stdout ]; then
		curl --fail --progress --$FILE_APPEND `echo $FILE_URL | sed 's,.*/,,'` $FILE_URL
	else
		curl --fail --silent $FILE_URL
	fi
elif [ $FILE_DOWN_MANAGER == "quiet" ]; then
	[ $2 == stdout ] || curl --fail --$FILE_APPEND `echo $FILE_URL | sed 's,.*/,,'` $FILE_URL &>/dev/null
fi
[ $? == 0 ] || exit 5
return 0
}

## Refreshes the repository
# $1 == Repository URL
# $2 == Cache Dir
slapt_refresh_repository()
{
OLDDIR=$PWD
REPO_URL=$1
STD_CACHE=$2
REPO_CACHE="$STD_CACHE/repositories/`echo $REPO_URL | sed -e s,http://,, -e s,ftp://,,`"
[ -d $REPO_CACHE ] || mkdir -p $REPO_CACHE
cd $REPO_CACHE
[ -z "`ls -1`" ] || rm -f ./*
slax_print "Retrieving package data for $(expr substr $1 1 40)..."
for datafile in PACKAGES.TXT CHECKSUMS.md5; do
	slapt_retrieve_file $1/$datafile output quiet
	[ $? == "5" ] && slax_bail "Not a valid repository"
done
slapt_retrieve_file $1/FILELIST.TXT output quiet
cd $OLDDIR
}

## Get a package section of a file
# $1 == Package
# $2 == Cache
# $3 == Config file
# $4 == loose/strict
slapt_package_selection()
{
OLDDIR=$PWD
APP=$1
CACHE_DIR=$2
CONFIG_FILE=$3
SEARCH_TYPE=$4
REPO_DIRS_TO_SEARCH=`grep -i SOURCE $CONFIG_FILE | sed 's,SOURCE=,,' | sed -e s,http://,, -e s,ftp://,,`
cd $CACHE_DIR/repositories
for dirtosearch in `echo $REPO_DIRS_TO_SEARCH`; do
	if [ $SEARCH_TYPE == "loose" ]; then
		PACKAGE_LIST_FOR_LOOP=`cat $dirtosearch/PACKAGES.TXT | grep -in $APP | grep "PACKAGE NAME:" | sed 's, ,@,g'`
	else
		PACKAGE_LIST_FOR_LOOP=`cat $dirtosearch/PACKAGES.TXT | grep -in $APP | grep "PACKAGE NAME:" | grep -iw $APP | grep -i "PACKAGE NAME: *$APP-[0-9]\|PACKAGE NAME: *$APP-SVN\|PACKAGE NAME: *$APP-CVS\|PACKAGE NAME: *$APP-BZR\|PACKAGE NAME: *$APP-GIT" | sed 's, ,@,g'`
	fi
	for package in $PACKAGE_LIST_FOR_LOOP; do
		package=$(echo $package | sed 's|@| |g')
		PKG_NAME=$(echo $package |  sed -e s,.tgz,, -e s,.*:,, -e s,\ ,,g -e s,-[0-9].[0-9].*,, -e s,-svn.*,,i)
		END_LINE=$(cat $dirtosearch/PACKAGES.TXT | grep -in $PKG_NAME: | cut -d: -f1 | tail -1)
		START_LINE=$(echo $package | cut -d: -f1)
		perl -ne "print $_ if ($START_LINE..$END_LINE);" $dirtosearch/PACKAGES.TXT
		echo "PACKAGE SERVER: $(cat $CONFIG_FILE | grep $dirtosearch | sed 's|SOURCE=||')"
		echo
## Alternative method via awk/gawk
#		for linenumber in `seq $START_LINE $END_LINE 2>/dev/null`; do
#			awk "FNR == $linenumber" $dirtosearch/PACKAGES.TXT
#		done
	done
done
cd $OLDDIR
}

## Search for a app
# $1 == App
# $2 == Cache Dir
# $3 == Config file
slapt_search_app()
{
OLDDIR=$PWD
APP=$1
CACHE_DIR=$2
CONFIG_FILE=$3
REPO_DIRS_TO_SEARCH=`grep -i SOURCE $CONFIG_FILE | sed 's,SOURCE=,,' | sed -e s,http://,, -e s,ftp://,,`
cd $CACHE_DIR/repositories
for dirtosearch in `echo $REPO_DIRS_TO_SEARCH`; do
	for package in `cat $dirtosearch/PACKAGES.TXT | grep -i $APP | grep "PACKAGE NAME:" |  sed -e s,.tgz,, -e s,PACKAGE\ NAME:,, -e s,\ ,,g -e s,-[0-9].[0-9].*,, -e s,-svn.*,,i`; do
		echo PKGNAME$package
		echo PKGSUMM`cat $dirtosearch/PACKAGES.TXT | grep -i "$package: $package" |  sed -e s,$package:\ ,,`
		echo
	done
done
cd $OLDDIR
}

## Resolves whether a dependency is solved
# $@ == Dependency string
# stdin == Results to check
slapt_resolve_dep()
{
DEP=$@
while read string; do
	for dep in `echo $DEP | sed 's| |SpAcE|g' | sed 's:|: :g'`; do
		dep=$(echo $dep | sed 's|SpAcE||g' | sed 's| ||')
		if ! (echo $dep | grep -e \> -e \< -e \=) &>/dev/null; then
			if (echo $string | grep -i -e ^$dep-[0-9] -e ^$dep-svn -e ^$dep-bzr -e ^$dep-git -e ^$dep-cvs -e ^$dep-rev) &>/dev/null; then
				echo $string
			fi
		else
			dep_name=$(echo $dep | sed -e s:\>.*:: -e s:\<.*:: -e s:=.*:: )
			if (echo $dep | grep -e \>= -e =\> -e \<= -e =\< ) &>/dev/null; then
				COMPARISON=`expr substr $(echo $dep | sed -e s:${dep_name}::) 1 2`
			else
				COMPARISON=`expr substr $(echo $dep | sed -e s:${dep_name}::) 1 1`
			fi
			if (echo $dep | grep -e \>= -e =\> -e \<= -e =\< ) &>/dev/null; then
				VERSION=$(echo $dep | sed -e s:${dep_name}..::)
			else
				VERSION=$(echo $dep | sed -e s:${dep_name}.::)
			fi
			if [ $COMPARISON != '>=' -a $COMPARISON != '=>' -a $COMPARISON != '<=' -a $COMPARISON != '=<' ] &>/dev/null; then
				if (echo $string | grep -i -e ^$dep_name-[0-9] \
				     -e ^$dep_name-svn -e ^$dep_name-bzr -e ^$dep_name-git -e ^$dep_name-cvs -e ^$dep_name-rev &>/dev/null) && \
				[ $(echo $string | sed -e s:$dep_name-::) $COMPARISON $VERSION ]
				then
					echo $string
				fi
			elif [ $COMPARISON == '>=' -o $COMPARISON == '=>' ] &>/dev/null; then
				if (echo $string | grep -i -e ^$dep_name-[0-9] \
				    -e ^$dep_name-svn -e ^$dep_name-bzr -e ^$dep_name-git -e ^$dep_name-cvs -e ^$dep_name-rev &>/dev/null) && \
				[ $(echo $(echo $string | sed -e s:$dep_name-:: | cut -d- -f1) $(echo $VERSION | cut -d- -f1) | awk '{print ($1 >= $2) ? "0" : "1"}') == "0" ]
				then
					echo $string
				fi				
			elif [ $COMPARISON == '<=' -o $COMPARISON == '=<' ] &>/dev/null; then
				if (echo $string | grep -i -e ^$dep_name-[0-9] \
				    -e ^$dep_name-svn -e ^$dep_name-bzr -e ^$dep_name-git -e ^$dep_name-cvs -e ^$dep_name-rev &>/dev/null) && \
				[ $(echo $(echo $string | sed -e s:$dep_name-:: | cut -d- -f1) $(echo $VERSION | cut -d- -f1) | awk '{print ($1 <= $2) ? "0" : "1"}') == "0" ]
				then
					echo $string
				fi
			fi
		fi
	done
done
}

## Resolve local dependency
# $1 == Dependency
# $2 == Packages Data dir
slapt_resolve_local()
{
DEPENDENCY_STRING=$1
PACKAGES_DATA_DIR=$2
# Forward this to slapt_resolve_dep
ls $PACKAGES_DATA_DIR | slapt_resolve_dep "$DEPENDENCY_STRING"
}

## Resolve internet dependency
# $1 == Dependency
# $2 == Config file
# $3 == Cache dir
slapt_resolve_remote()
{
DEPENDENCY_STRING=$1
CONFIG_FILE=$2
CACHE_DIR=$3
# Forward this to slapt_resolve_dep
(for list in $(cat $CONFIG_FILE | sed 's|SOURCE=.*//||'); do
	cat $CACHE_DIR/repositories/$list/PACKAGES.TXT | grep PACKAGE\ NAME: | sed 's|PACKAGE NAME:||' | sed 's|.tgz||'
done) | slapt_resolve_dep "$DEPENDENCY_STRING"
}

## Download a package
# $1 == Package
# $2 == Cache
# $3 == Config file
slapt_get_package()
{
PACKAGE=$1
CACHE=$2
CONFIG=$3
slapt_package_selection $PACKAGE $CACHE $CONFIG strict >/tmp/parseslapt.tmp
if (grep PACKAGE\ MIRROR: /tmp/parseslapt.tmp) &>/dev/null; then
	URL=$(grep PACKAGE\ MIRROR: /tmp/parseslapt.tmp | sed 's|PACKAGE MIRROR:||' | sed 's| ||g')/$(grep PACKAGE\ LOCATION: /tmp/parseslapt.tmp | sed 's|PACKAGE LOCATION:||' | sed 's| ||g')/$(grep PACKAGE\ NAME: /tmp/parseslapt.tmp | sed 's|PACKAGE NAME:||' | sed 's| ||g')
else
	URL=$(grep PACKAGE\ SERVER: /tmp/parseslapt.tmp | sed 's|PACKAGE SERVER:||' | sed 's| ||g')/$(grep PACKAGE\ LOCATION: /tmp/parseslapt.tmp | sed 's|PACKAGE LOCATION:||' | sed 's| ||g')/$(grep PACKAGE\ NAME: /tmp/parseslapt.tmp | sed 's|PACKAGE NAME:||' | sed 's| ||g')
fi
[ -d "$CACHE/packages" ] || mkdir -p $CACHE/packages
slax_print "Downloading $(expr substr $URL 1 35)...$(grep PACKAGE\ NAME: /tmp/parseslapt.tmp | sed 's|PACKAGE NAME:||' | sed 's| ||g')"
cd $CACHE/packages; slapt_retrieve_file $URL output verbose; cd $OLDPWD
rm -f /tmp/parseslapt.tmp
}
